package com.xhwl.centiotdata.handler.visitor.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.xhwl.centiotdata.emq.MqttProperties;
import com.xhwl.centiotdata.emq.QosEnum;
import com.xhwl.centiotdata.handler.visitor.VisitorIntegrationFactory;
import com.xhwl.centiotdata.handler.visitor.VisitorIntegrationHandler;
import com.xhwl.centiotdata.pojo.dto.haikang.VisitorInfoHaikangDTO;
import com.xhwl.centiotdata.pojo.dto.visitor.VisitorIntegrationConfigDTO;
import com.xhwl.centiotdata.pojo.dto.yunping.VisitorInfoYunPingDTO;
import com.xhwl.centiotdata.service.IEmqService;
import com.xhwl.centiotdata.service.cent.IAuthAccountCentService;
import com.xhwl.centiotdata.suppliersystem.haikang.VistorHaikangHandler;
import com.xhwl.centiotdata.utils.Base64Utils;
import com.xhwl.common.dto.cent.visitor.VisitorAuthDTO;
import com.xhwl.common.dto.cent.visitor.VisitorInfoDTO;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.visitor.Follower;
import com.xhwl.common.utils.StringUtils;
import com.xhwl.common.utils.function.MyStringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author pengxiao
 * @description
 * @date 2022/6/10 17:02
 */
@Service
@Slf4j
public class VisitorIntegrationYunPingHandler extends VisitorIntegrationHandler {

    @Resource
    private IEmqService emqService;

    @Autowired
    private MqttProperties mqttProperties;

    @Override
    public void afterPropertiesSet() throws Exception {
        VisitorIntegrationFactory.register(7, this);
    }

    @Override
    public ResultJson createVisitorAuth(VisitorIntegrationConfigDTO configDTO, VisitorAuthDTO authDTO) {

        VisitorInfoYunPingDTO dto = new VisitorInfoYunPingDTO();
        dto.setPhone(authDTO.getPhone());
        dto.setVisitorName(authDTO.getName());
        dto.setFaceUrl(authDTO.getFaceUrl());
        dto.setFeature(authDTO.getFeature());
        dto.setReason(authDTO.getSubjectMatter());
        dto.setOpenDeviceIdList(authDTO.getOpenDeviceIdList());
        dto.setIdentityCard(authDTO.getIdentityCard());
        dto.setIdentityCardType(authDTO.getIdentityCardType());
        dto.setVisitorId(authDTO.getId());
        dto.setProjectId(authDTO.getProjectId());
        dto.setAuthStartTime(authDTO.getAuthStartTime());
        dto.setAuthEndTime(authDTO.getAuthEndTime());

        log.info("下发云屏访客权限,参数:{}", MyStringUtils.objectToJson(dto));
        emqService.sendToEmp(mqttProperties.getVisitorTopic(), String.valueOf(authDTO.getProjectId()), dto);

        return ResultJson.success();
    }

    @Override
    public ResultJson removeVisitorAuth(VisitorIntegrationConfigDTO configDTO, VisitorInfoDTO authDTO){

        VisitorInfoYunPingDTO dto = new VisitorInfoYunPingDTO();
        dto.setPhone(authDTO.getPhone());
        dto.setVisitorName(authDTO.getName());
        dto.setFaceUrl(authDTO.getFaceUrl());
        dto.setReason(authDTO.getSubjectMatter());
        dto.setIdentityCard(authDTO.getIdentityCard());
        dto.setIdentityCardType(authDTO.getIdentityCardType());
        dto.setVisitorId(authDTO.getId());
        dto.setProjectId(authDTO.getProjectId());

        log.info("删除云屏访客权限,参数:{}", MyStringUtils.objectToJson(dto));
        emqService.sendToEmp(mqttProperties.getVisitorTopic(), String.valueOf(authDTO.getProjectId()), dto);

        return ResultJson.success();
    }
}
